package znbkTestPaperXtfx.knowledgeList;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.view.base.BaseActivity;
import com.example.lancooznbk.view.widget.ScaleButton;

import lancooControl.AndroidBug5497Workaround;

import static znbkTestPaperXtfx.AnswerSheet.arrayKnowledgeNames;
import static znbkTestPaperXtfx.AnswerSheet.arrayLastScores;
import static znbkTestPaperXtfx.AnswerSheet.arrayOriginalScores;
import static znbkTestPaperXtfx.AnswerSheet.arrayRequiredScores;

/**
 * Created by Administrator on 2018\4\9 0009.
 */

public class KnowledgesListActivity extends BaseActivity{

    private knowledgesListAdapter mAdapter;
    @Override
    public int attachLayoutRes() {
        return R.layout.znbk_knowledges_list;
    }

    @Override
    protected void initViews() {

        AndroidBug5497Workaround.assistActivity(this);
        //核心代码.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        RelativeLayout relToolbar = (RelativeLayout) findViewById(R.id.rlayout_back);
        int statusBarHeight = getStatusBarHeight();
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) relToolbar.getLayoutParams();
        layoutParams.setMargins(0,statusBarHeight,0,0);

        ScaleButton mSBtnBack = (ScaleButton) findViewById(R.id.sbtn_back);
        mSBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        RecyclerView mRvKnowledges = (RecyclerView) findViewById(R.id.rv_knowledges);
        mRvKnowledges.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new knowledgesListAdapter();
        mRvKnowledges.setAdapter(mAdapter);

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    public class knowledgesListAdapter extends RecyclerView.Adapter<knowledgesListAdapter.MyViewHolder> {
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(KnowledgesListActivity.this).
                    inflate(R.layout.znbk_knowledges_list_item,parent,false));
            return holder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            /*
            * 显示考点
            * */
            holder.mTvKnowledge.setText(arrayKnowledgeNames.get(position));

             /*
            * 计算当前得分是进步还是退步
            * */
            double changeScore = 0;
            int changeFlag = 0;
            if(Double.valueOf(arrayLastScores.get(position))>Double.valueOf(arrayOriginalScores.get(position))){
                changeScore = Double.valueOf(arrayLastScores.get(position))-Double.valueOf(arrayOriginalScores.get(position));
                changeFlag = 1;
            }else if(Double.valueOf(arrayLastScores.get(position))==Double.valueOf(arrayOriginalScores.get(position))){
                changeScore = 0;
                changeFlag = 0;
            }else if(Double.valueOf(arrayLastScores.get(position))<Double.valueOf(arrayOriginalScores.get(position))){
                changeScore = Double.valueOf(arrayOriginalScores.get(position))-Double.valueOf(arrayLastScores.get(position));
                changeFlag = -1;
            }
            if(changeFlag==-1){
                Drawable drawable = getResources().getDrawable(R.mipmap.znbk_icon_score_down);
                drawable.setBounds(0,0,drawable.getMinimumHeight(),drawable.getMinimumHeight());
                holder.mTvChangeScore.setCompoundDrawables(null,null,drawable,null);
            }else if(changeFlag==0){
                holder.mTvChangeScore.setCompoundDrawables(null,null,null,null);
            }else if(changeFlag==1){
                Drawable drawable = getResources().getDrawable(R.mipmap.znbk_icon_score_up);
                drawable.setBounds(0,0,drawable.getMinimumHeight(),drawable.getMinimumHeight());
                holder.mTvChangeScore.setCompoundDrawables(null,null,drawable,null);
            }
            String strChangeScore = "";
            if(changeScore==0){
                strChangeScore = "0";
            }else{
                strChangeScore = String.valueOf(changeScore);
            }
            holder.mTvChangeScore.setText(strChangeScore);

            /*
            * 显示达标分
            * */
            holder.mTvRequiredScore.setText("（ 当前考点达标得分为"+arrayRequiredScores.get(position)+" ）");

        }

        @Override
        public int getItemCount() {
            return arrayKnowledgeNames.size();
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {

            TextView mTvKnowledge;
            TextView mTvChangeScore;
            TextView mTvRequiredScore;

            public MyViewHolder(View itemView) {
                super(itemView);
                mTvKnowledge = (TextView) itemView.findViewById(R.id.tv_knowledge);
                mTvChangeScore = (TextView) itemView.findViewById(R.id.tv_changeScore);
                mTvRequiredScore = (TextView) itemView.findViewById(R.id.tv_requiredScore);
            }
        }

    }


}
